package com.tgr.shortLink.admin.common.convention.result;

import com.tgr.shortLink.admin.common.convention.errorCode.BaseErrorCode;
import com.tgr.shortLink.admin.common.convention.errorCode.IErrorCode;
import com.tgr.shortLink.admin.common.convention.exception.AbstractException;
import com.tgr.shortLink.admin.dto.resp.UserRespDTO;

import java.util.Optional;

/**
 * 描述：全局返回对象构造器
 * 作者：小陶不慌张
 * 文件：Results
 * 日期：2023/12/22 20:50
 */

public class Results {

    /**
     * 构造成功响应
     */
    public static Result<Void> success(){
        return new Result<Void>().setCode(Result.SUCCESS_CODE);
    }

    /**
     * 构造带返回数据的成功响应
     */
    public static <T> Result<T> success(T data){
        return new Result<T>()
                .setCode(Result.SUCCESS_CODE)
                .setData(data);
    }
    public static <T> Result<T> success(T data,String message){
        return new Result<T>()
                .setCode(Result.SUCCESS_CODE)
                .setData(data)
                .setMessage(message);
    }

    /**
     * 构建服务端失败响应
     */
    public  static Result<Void> failure(){
        return new Result<Void>()
                .setCode(BaseErrorCode.SERVICE_ERROR.code())
                .setMessage(BaseErrorCode.SERVICE_ERROR.message());
    }

    /**
     * 通过{@link AbstractException} 构建失败响应
     */
    public static Result<Void> failure(AbstractException abstractException){
        String errorCode = Optional.ofNullable(abstractException.getErrorCode())
                .orElse(BaseErrorCode.SERVICE_ERROR.code());
        String errorMessage = Optional.ofNullable(abstractException.getErrorMessage())
                .orElse((BaseErrorCode.SERVICE_ERROR.message()));
        return new Result<Void>()
                .setCode(errorCode)
                .setMessage(errorMessage);
    }

    /**
     * 通过errorCode,errorMessage构建失败响应
     */
    public static Result<Void> failure(String errorCode, String errorMessage){
        return new Result<Void>()
                .setCode(errorCode)
                .setMessage(errorMessage);
    }

}
